package com.example.demo.utils;

import com.example.demo.dto.common.ConsultHistoryModel;
import com.example.demo.dto.common.ConsultantModel;
import com.example.demo.entity.Consultant;
import com.example.demo.entity.Supervisor;
import com.example.demo.entity.relation.ConsultRecord;

import java.util.Optional;

public class InputModelUtil {
    private static final String EMPTY = "";
    private static final String SCHEDULE_DEFAULT = "未排班";
    private static final String EVALUATION_DEFAULT = "5";
    private static final String MINUTES_DEFAULT = "0";
    private static final String COUNT_DEFAULT = "0";

    /**
     * ConsultantModel 相关
     */

    /**
     * Consultant 转换成 ConsultantModel
     * @param consultant
     * @return
     */
    public static ConsultantModel transToModelOfConsultant(Consultant consultant) {
        Optional<Consultant> origin = Optional.ofNullable(consultant);
        ConsultantModel model = new ConsultantModel();
        model.setIsSupervise(String.valueOf(false));
        model.setId(origin.map(Consultant::getId).orElse(EMPTY));
        model.setName(origin.map(Consultant::getName).orElse(EMPTY));
        model.setAge(String.valueOf(origin.map(Consultant::getAge).orElse((short)0)));
        model.setCompany(origin.map(Consultant::getCompany).orElse(EMPTY));
        model.setEmail(origin.map(Consultant::getEmail).orElse(EMPTY));
        model.setTitle(origin.map(Consultant::getTitle).orElse(EMPTY));
        model.setSchedule(origin.map(Consultant::getSchedule).orElse(SCHEDULE_DEFAULT));
        model.setPhoneNum(origin.map(Consultant::getPhoneNumber).orElse(EMPTY));
        model.setGender(origin.map(Consultant::getGender).orElse(EMPTY));
        model.setAverageEvaluation(origin.map(Consultant::getEvaluation).orElse(EVALUATION_DEFAULT));
        model.setTotalMinutes(origin.map(Consultant::getTotalMinutes).orElse(MINUTES_DEFAULT));

        return model;
    }

    /**
     * Consultant，Supervisor 转化成一个带 Supervisor信息的 ConsultantModel
     * @param consultant
     * @param supervisor
     * @return
     */
    public static ConsultantModel transToModelOfSupervisor(Consultant consultant, Supervisor supervisor) {
        ConsultantModel model = transToModelOfConsultant(consultant);
        Optional<Supervisor> origin = Optional.ofNullable(supervisor);
        model.setIsSupervise(String.valueOf(true));
        model.setSuperviseCount(origin.map(Supervisor::getSuperviseCount).orElse(COUNT_DEFAULT));
        model.setSuperviseTotalMinutes(origin.map(Supervisor::getSuperviseTime).orElse(MINUTES_DEFAULT));

        return model;
    }

    /**
     * 从ConsultantModel 转变成 Consultant
     * 属性值原来若为空串，则转变成 null。
     * @param model
     * @return
     */
    public static Consultant transToConsultant(ConsultantModel model) {
        if (model == null) {
            return null;
        }

        class StringDefaultValue {
            public String defaultValue(String str) {
                return (str == null || str.equals(EMPTY)) ? null : str;
            }
        }

        StringDefaultValue stringHelper = new StringDefaultValue();
        Consultant consultant = new Consultant();
        consultant.setId(stringHelper.defaultValue(model.getId()));
        consultant.setName(stringHelper.defaultValue(model.getName()));
        consultant.setPhoneNumber(stringHelper.defaultValue(model.getPhoneNum()));
        consultant.setTotalMinutes(stringHelper.defaultValue(model.getTotalMinutes()));
        consultant.setEvaluation(stringHelper.defaultValue(model.getAverageEvaluation()));
        consultant.setIsSupervisor(stringHelper.defaultValue(model.getIsSupervise()));
        consultant.setSchedule(stringHelper.defaultValue(model.getSchedule()));
        consultant.setPassword(stringHelper.defaultValue(model.getPassword()));
        consultant.setTitle(stringHelper.defaultValue(model.getTitle()));
        consultant.setCompany(stringHelper.defaultValue(model.getCompany()));
        consultant.setGender(stringHelper.defaultValue(model.getGender()));
        consultant.setEmail(stringHelper.defaultValue(model.getEmail()));
        consultant.setAge(
                stringHelper.defaultValue(model.getAge()) == null ?
                        (short) 0 :
                        Short.parseShort(model.getAge())
        );

        return consultant;
    }

    public static ConsultHistoryModel transToConsultHistory(ConsultRecord record) {
        Optional<ConsultRecord> origin = Optional.ofNullable(record);

        ConsultHistoryModel history = new ConsultHistoryModel();
        history.setId(origin.map(ConsultRecord::getId).orElse(EMPTY));
        history.setDuration(origin.map(ConsultRecord::getDuration).orElse(MINUTES_DEFAULT));
        history.setTime(origin.map(ConsultRecord::getTime).orElse(EMPTY));
        history.setHadEvaluated(origin.map(ConsultRecord::getHadEvaluated).orElse(String.valueOf(false)));
        history.setConsultType(origin.map(ConsultRecord::getConsultType).orElse(EMPTY));
        history.setEvaluateScore(origin.map(ConsultRecord::getEvaluateScore).orElse(EVALUATION_DEFAULT));
        history.setEvaluateText(origin.map(ConsultRecord::getEvaluateText).orElse(EMPTY));

        return history;
    }
}
